using Domain.Entities;
using Domain.Entities.Enums;
using Domain.Services;
using System;
using System.Text;
using System.Collections.Generic;
using System.Web.UI.WebControls;

namespace UserInterface.Admin
{
    public partial class EditUserPermissions : System.Web.UI.Page
    {
        private int userId;

        protected void Page_Load(object sender, EventArgs e)
        {
            userId = Convert.ToInt16(Request.QueryString["userId"]);
            
            if (!IsPostBack)
            {
                User user = UserService.GetById(userId);
                UserNameLiteral.Text = user.Username;

                GroupsListBox.DataSource = GroupService.GetAll();
                GroupsListBox.DataBind();

                foreach (ListItem li in GroupsListBox.Items)
                {
                    foreach (Group g in user.Groups)
                    {
                        if (g.Id == Convert.ToInt16(li.Value))
                        {
                            li.Selected = true;
                        }
                    }
                }

                ForumsRepeater.DataSource = ForumService.GetAll();
                ForumsRepeater.DataBind();
            }
        }

        protected void SaveButton_Click(object sender, EventArgs e)
        {
            User user = UserService.GetById(userId);
            IList<Group> groups = user.Groups;

            foreach (ListItem li in GroupsListBox.Items)
            {
                Group group = GroupService.GetById(Convert.ToInt16(li.Value));

                if (li.Selected)
                {
                    if (!groups.Contains(group))
                    {
                        groups.Add(group);
                    }
                }
                else
                {
                    if (groups.Contains(group))
                    {
                        groups.Remove(group);
                    }
                }
            }

            UserService.UpdateGroups(user, groups);

            foreach (RepeaterItem item in ForumsRepeater.Items)
            {
                short forumId = Convert.ToInt16((item.FindControl("ForumIdHiddenField") as HiddenField).Value);
                CheckBox readCheckBox = item.FindControl("ReadCheckBox") as CheckBox;
                CheckBox replyCheckBox = item.FindControl("ReplyCheckBox") as CheckBox;
                CheckBox voteCheckBox = item.FindControl("VoteCheckBox") as CheckBox;
                CheckBox createTopicCheckBox = item.FindControl("CreateTopicCheckBox") as CheckBox;
                CheckBox createPollCheckBox = item.FindControl("CreatePollCheckBox") as CheckBox;
                CheckBox createStickyCheckBox = item.FindControl("CreateStickyCheckBox") as CheckBox;
                CheckBox createAnnouncementCheckBox = item.FindControl("CreateAnnouncementCheckBox") as CheckBox;
                CheckBox createFaqCheckBox = item.FindControl("CreateFaqCheckBox") as CheckBox;
                CheckBox editOwnCheckBox = item.FindControl("EditOwnCheckBox") as CheckBox;
                CheckBox deleteOwnCheckBox = item.FindControl("DeleteOwnCheckBox") as CheckBox;
                CheckBox moderateCheckBox = item.FindControl("ModerateCheckBox") as CheckBox;

                Permission permissions = Permission.None;

                if (readCheckBox.Checked)
                {
                    permissions = permissions |= Permission.Read;
                }
                if (replyCheckBox.Checked)
                {
                    permissions = permissions |= Permission.Reply;
                }
                if (voteCheckBox.Checked)
                {
                    permissions = permissions |= Permission.Vote;
                }
                if (createTopicCheckBox.Checked)
                {
                    permissions = permissions |= Permission.CreateTopic;
                }
                if (createPollCheckBox.Checked)
                {
                    permissions = permissions |= Permission.CreatePoll;
                }
                if (createStickyCheckBox.Checked)
                {
                    permissions = permissions |= Permission.CreateSticky;
                }
                if (createAnnouncementCheckBox.Checked)
                {
                    permissions = permissions |= Permission.CreateAnnouncement;
                }
                if (createFaqCheckBox.Checked)
                {
                    permissions = permissions |= Permission.CreateFaq;
                }
                if (editOwnCheckBox.Checked)
                {
                    permissions = permissions |= Permission.EditOwn;
                }
                if (deleteOwnCheckBox.Checked)
                {
                    permissions = permissions |= Permission.DeleteOwn;
                }
                if (moderateCheckBox.Checked)
                {
                    permissions = permissions |= Permission.Moderate;
                }

                UserForumPermission userForumPermission = UserForumPermissionService.GetByUserIdAndForumId(userId, forumId);
                if (userForumPermission == null)
                {
                    if (permissions != Permission.None)
                    {
                        Forum forum = ForumService.GetById(forumId);
                        UserForumPermissionService.Add(user, forum, permissions);
                    }
                }
                else
                {
                    if (permissions != Permission.None)
                    {
                        UserForumPermissionService.Update(userForumPermission, permissions);
                    }
                    else
                    {
                        UserForumPermissionService.Delete(userForumPermission);
                    }
                }
            }
        }

        protected void ForumsRepeater_ItemCreated(object sender, RepeaterItemEventArgs e)
        {
            if (!IsPostBack)
            {
                if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
                {
                    CheckBox readCheckBox = e.Item.FindControl("ReadCheckBox") as CheckBox;
                    CheckBox replyCheckBox = e.Item.FindControl("ReplyCheckBox") as CheckBox;
                    CheckBox voteCheckBox = e.Item.FindControl("VoteCheckBox") as CheckBox;
                    CheckBox createTopicCheckBox = e.Item.FindControl("CreateTopicCheckBox") as CheckBox;
                    CheckBox createPollCheckBox = e.Item.FindControl("CreatePollCheckBox") as CheckBox;
                    CheckBox createStickyCheckBox = e.Item.FindControl("CreateStickyCheckBox") as CheckBox;
                    CheckBox createAnnouncementCheckBox = e.Item.FindControl("CreateAnnouncementCheckBox") as CheckBox;
                    CheckBox createFaqCheckBox = e.Item.FindControl("CreateFaqCheckBox") as CheckBox;
                     CheckBox editOwnCheckBox = e.Item.FindControl("EditOwnCheckBox") as CheckBox;
                    CheckBox deleteOwnCheckBox = e.Item.FindControl("DeleteOwnCheckBox") as CheckBox;
                    CheckBox moderateCheckBox = e.Item.FindControl("ModerateCheckBox") as CheckBox;

                    UserForumPermission userForumPermission = UserForumPermissionService.GetByUserIdAndForumId(userId, (e.Item.DataItem as Forum).Id);
                    if (userForumPermission != null)
                    {
                        readCheckBox.Checked = ((userForumPermission.Permissions & Permission.Read) != 0);
                        replyCheckBox.Checked = ((userForumPermission.Permissions & Permission.Reply) != 0);
                        voteCheckBox.Checked = ((userForumPermission.Permissions & Permission.Vote) != 0);
                        createTopicCheckBox.Checked = ((userForumPermission.Permissions & Permission.CreateTopic) != 0);
                        createPollCheckBox.Checked = ((userForumPermission.Permissions & Permission.CreatePoll) != 0);
                        createStickyCheckBox.Checked = ((userForumPermission.Permissions & Permission.CreateSticky) != 0);
                        createAnnouncementCheckBox.Checked = ((userForumPermission.Permissions & Permission.CreateAnnouncement) != 0);
                        createFaqCheckBox.Checked = ((userForumPermission.Permissions & Permission.CreateFaq) != 0);
                        editOwnCheckBox.Checked = ((userForumPermission.Permissions & Permission.EditOwn) != 0);
                        deleteOwnCheckBox.Checked = ((userForumPermission.Permissions & Permission.DeleteOwn) != 0);
                        moderateCheckBox.Checked = ((userForumPermission.Permissions & Permission.Moderate) != 0);
                    }
                }
            }
        }

        protected void ForumsRepeater_PreRender(object sender, EventArgs e)
        {
            foreach (RepeaterItem repeaterItem in (sender as Repeater).Items)
            {
                Image toggleCheckAllImage = repeaterItem.FindControl("ToggleCheckAllImage") as Image;

                CheckBox readCheckBox = repeaterItem.FindControl("ReadCheckBox") as CheckBox;
                CheckBox replyCheckBox = repeaterItem.FindControl("ReplyCheckBox") as CheckBox;
                CheckBox voteCheckBox = repeaterItem.FindControl("VoteCheckBox") as CheckBox;
                CheckBox createTopicCheckBox = repeaterItem.FindControl("CreateTopicCheckBox") as CheckBox;
                CheckBox createPollCheckBox = repeaterItem.FindControl("CreatePollCheckBox") as CheckBox;
                CheckBox createStickyCheckBox = repeaterItem.FindControl("CreateStickyCheckBox") as CheckBox;
                CheckBox createAnnouncementCheckBox = repeaterItem.FindControl("CreateAnnouncementCheckBox") as CheckBox;
                CheckBox createFaqCheckBox = repeaterItem.FindControl("CreateFaqCheckBox") as CheckBox;
                CheckBox editOwnCheckBox = repeaterItem.FindControl("EditOwnCheckBox") as CheckBox;
                CheckBox deleteOwnCheckBox = repeaterItem.FindControl("DeleteOwnCheckBox") as CheckBox;
                CheckBox moderateCheckBox = repeaterItem.FindControl("ModerateCheckBox") as CheckBox;

                replyCheckBox.Attributes.Add("onclick", "setChecked(true, '" + readCheckBox.ClientID + "', this);");
                voteCheckBox.Attributes.Add("onclick", "setChecked(true, '" + readCheckBox.ClientID + "', this);");
                createTopicCheckBox.Attributes.Add("onclick", "setChecked(true, '" + readCheckBox.ClientID + "', this);");
                createPollCheckBox.Attributes.Add("onclick", "setChecked(true, '" + readCheckBox.ClientID + ";" + createTopicCheckBox.ClientID + "', this);");
                createStickyCheckBox.Attributes.Add("onclick", "setChecked(true, '" + readCheckBox.ClientID + ";" + createTopicCheckBox.ClientID + "', this);");
                createAnnouncementCheckBox.Attributes.Add("onclick", "setChecked(true, '" + readCheckBox.ClientID + ";" + createTopicCheckBox.ClientID + "', this);");
                createFaqCheckBox.Attributes.Add("onclick", "setChecked(true, '" + readCheckBox.ClientID + ";" + createTopicCheckBox.ClientID + "', this);");
                editOwnCheckBox.Attributes.Add("onclick", "setChecked(true, '" + readCheckBox.ClientID + ";" + replyCheckBox.ClientID + "', this);");
                deleteOwnCheckBox.Attributes.Add("onclick", "setChecked(true, '" + readCheckBox.ClientID + ";" + replyCheckBox.ClientID + "', this);");
                moderateCheckBox.Attributes.Add("onclick", "setChecked(true, '" + readCheckBox.ClientID + ";" + replyCheckBox.ClientID + "', this);");

                StringBuilder sb = new StringBuilder();

                sb.Append("'" + readCheckBox.ClientID + "',");
                sb.Append("'" + replyCheckBox.ClientID + "',");
                sb.Append("'" + voteCheckBox.ClientID + "',");
                sb.Append("'" + createTopicCheckBox.ClientID + "',");
                sb.Append("'" + createPollCheckBox.ClientID + "',");
                sb.Append("'" + createStickyCheckBox.ClientID + "',");
                sb.Append("'" + createAnnouncementCheckBox.ClientID + "',");
                sb.Append("'" + createFaqCheckBox.ClientID + "',");
                sb.Append("'" + editOwnCheckBox.ClientID + "',");
                sb.Append("'" + deleteOwnCheckBox.ClientID + "',");
                sb.Append("'" + moderateCheckBox.ClientID + "'");

                toggleCheckAllImage.Attributes.Add("onclick", "toggleCheckAll(" + sb + ")");
            }
        }
    }
}
